function res = resid_dim5case6_pscc(x,la,u,mu0,phi,kap0,eta0,be,del,d,Pzz,cz,T_guess,M)
%function solving for equilibrium (given lambda) in case where all firms actively sell in top three productivity 
%states, and no firm actively sells in other states

options = optimoptions('fsolve','TolFun',1e-9,'TolX',1e-9,'StepTolerance',1e-9,'Display','none');

shat_1=x(1);shat_2=x(2);shat_3=x(3);dim=5;

%solve for theta's corresponding to shat's 
th_res = @(th) la-mu_pscc(th,mu0,eta0)*eta_el_pscc(th,mu0,eta0)/(1-eta_el_pscc(th,mu0,eta0))*shat_1*kap_s_pscc(shat_1,kap0,phi)/(1+eta_pscc(th,mu0,eta0)*shat_1);
[th_1,fval,exit_flag1] = fsolve(th_res,T_guess(1),options);
th_res = @(th) la-mu_pscc(th,mu0,eta0)*eta_el_pscc(th,mu0,eta0)/(1-eta_el_pscc(th,mu0,eta0))*shat_2*kap_s_pscc(shat_2,kap0,phi)/(1+eta_pscc(th,mu0,eta0)*shat_2);
[th_2,fval,exit_flag2] = fsolve(th_res,T_guess(2),options);
th_res = @(th) la-mu_pscc(th,mu0,eta0)*eta_el_pscc(th,mu0,eta0)/(1-eta_el_pscc(th,mu0,eta0))*shat_3*kap_s_pscc(shat_3,kap0,phi)/(1+eta_pscc(th,mu0,eta0)*shat_3);
[th_3,fval,exit_flag3]= fsolve(th_res,T_guess(3),options);
if min([exit_flag1,exit_flag2,exit_flag3])<1,disp('issue: case 6');end

%match surpluses given active selling behavior
S_1=kap_s_pscc(shat_1,kap0,phi)/eta_pscc(th_1,mu0,eta0)+la/mu_pscc(th_1,mu0,eta0);
S_2=kap_s_pscc(shat_2,kap0,phi)/eta_pscc(th_2,mu0,eta0)+la/mu_pscc(th_2,mu0,eta0);
S_3=kap_s_pscc(shat_3,kap0,phi)/eta_pscc(th_3,mu0,eta0)+la/mu_pscc(th_3,mu0,eta0);

%match surpluses given behavior in active selling states and otherwise
S_vec=M*(u*ones(dim,1)-cz+be*(1-d)*(1-del)*(-la*ones(dim,1)-Pzz(:,1)*kap_n_pscc(shat_1,kap0,phi)-Pzz(:,2)*kap_n_pscc(shat_2,kap0,phi)-Pzz(:,3)*kap_n_pscc(shat_3,kap0,phi)));

%equilibrium consistency conditions for surplus
res= [S_1;S_2;S_3]-S_vec(1:3);
end